<template>
  <div class="blacklist">
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form :model="searchForm" ref="searchFormRef" inline>
        <el-form-item label="客户姓名" prop="customerName">
          <el-input
            v-model="searchForm.customerName"
            placeholder="请输入客户姓名"
            clearable
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input
            v-model="searchForm.idCard"
            placeholder="请输入身份证号"
            clearable
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="searchForm.phone"
            placeholder="请输入手机号码"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
            style="width: 120px"
          >
            <el-option label="生效中" value="active" />
            <el-option label="已解除" value="inactive" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="success" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 黑名单列表 -->
    <el-card class="table-card">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="customerName" label="客户姓名" width="120" />
        <el-table-column prop="idCard" label="身份证号" width="180" />
        <el-table-column prop="phone" label="手机号码" width="120" />
        <el-table-column prop="reason" label="拉黑原因" min-width="200" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'danger' : 'info'">
              {{ scope.row.status === 'active' ? '生效中' : '已解除' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="operator" label="操作人" width="120" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
            <el-button
              v-if="scope.row.status === 'active'"
              link
              type="success"
              @click="handleRemove(scope.row)"
            >
              解除
            </el-button>
            <el-button
              v-if="scope.row.status === 'inactive'"
              link
              type="danger"
              @click="handleRestore(scope.row)"
            >
              恢复
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog
      v-model="formVisible"
      :title="formType === 'add' ? '新增黑名单' : '编辑黑名单'"
      width="600px"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item label="客户姓名" prop="customerName">
          <el-input
            v-model="form.customerName"
            placeholder="请输入客户姓名"
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input
            v-model="form.idCard"
            placeholder="请输入身份证号"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号码"
          />
        </el-form-item>
        <el-form-item label="拉黑原因" prop="reason">
          <el-input
            v-model="form.reason"
            type="textarea"
            :rows="3"
            placeholder="请输入拉黑原因"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="formVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog
      v-model="detailVisible"
      title="黑名单详情"
      width="800px"
      destroy-on-close
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="客户姓名">{{ detail.customerName }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ detail.idCard }}</el-descriptions-item>
        <el-descriptions-item label="手机号码">{{ detail.phone }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="detail.status === 'active' ? 'danger' : 'info'">
            {{ detail.status === 'active' ? '生效中' : '已解除' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="拉黑原因" :span="2">{{ detail.reason }}</el-descriptions-item>
        <el-descriptions-item label="操作人">{{ detail.operator }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.createTime }}</el-descriptions-item>
        <el-descriptions-item v-if="detail.status === 'inactive'" label="解除人">{{ detail.remover }}</el-descriptions-item>
        <el-descriptions-item v-if="detail.status === 'inactive'" label="解除时间">{{ detail.removeTime }}</el-descriptions-item>
        <el-descriptions-item v-if="detail.status === 'inactive'" label="解除原因" :span="2">{{ detail.removeReason }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 解除弹窗 -->
    <el-dialog
      v-model="removeVisible"
      title="解除黑名单"
      width="600px"
      destroy-on-close
    >
      <el-form
        ref="removeFormRef"
        :model="removeForm"
        :rules="removeRules"
        label-width="100px"
      >
        <el-form-item label="解除原因" prop="reason">
          <el-input
            v-model="removeForm.reason"
            type="textarea"
            :rows="3"
            placeholder="请输入解除原因"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="removeVisible = false">取消</el-button>
          <el-button type="primary" @click="handleRemoveSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  customerName: '',
  idCard: '',
  phone: '',
  status: ''
})

// 表格数据
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)
const tableData = ref([
  {
    customerName: '张三',
    idCard: '440300199001011234',
    phone: '13800138000',
    reason: '多次逾期，信用较差',
    status: 'active',
    operator: '李四',
    createTime: '2024-03-01 10:00:00'
  }
])

// 表单数据
const formVisible = ref(false)
const formRef = ref(null)
const formType = ref('add')
const form = reactive({
  customerName: '',
  idCard: '',
  phone: '',
  reason: ''
})

const formRules = {
  customerName: [
    { required: true, message: '请输入客户姓名', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' }
  ],
  reason: [
    { required: true, message: '请输入拉黑原因', trigger: 'blur' }
  ]
}

// 解除数据
const removeVisible = ref(false)
const removeFormRef = ref(null)
const currentBlacklist = ref({})
const removeForm = reactive({
  reason: ''
})

const removeRules = {
  reason: [
    { required: true, message: '请输入解除原因', trigger: 'blur' }
  ]
}

// 详情数据
const detailVisible = ref(false)
const detail = ref({})

// 查询
const handleSearch = () => {
  loading.value = true
  // TODO: 实现查询逻辑
  setTimeout(() => {
    loading.value = false
  }, 500)
}

// 重置
const handleReset = () => {
  searchForm.customerName = ''
  searchForm.idCard = ''
  searchForm.phone = ''
  searchForm.status = ''
  handleSearch()
}

// 新增
const handleAdd = () => {
  formType.value = 'add'
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
  formVisible.value = true
}

// 提交
const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid, fields) => {
    if (valid) {
      // TODO: 实现提交逻辑
      ElMessage.success('提交成功')
      formVisible.value = false
      handleSearch()
    }
  })
}

// 解除
const handleRemove = (row) => {
  currentBlacklist.value = row
  removeForm.reason = ''
  removeVisible.value = true
}

// 提交解除
const handleRemoveSubmit = async () => {
  if (!removeFormRef.value) return
  
  await removeFormRef.value.validate((valid, fields) => {
    if (valid) {
      // TODO: 实现解除提交逻辑
      ElMessage.success('解除成功')
      removeVisible.value = false
      handleSearch()
    }
  })
}

// 恢复
const handleRestore = (row) => {
  ElMessageBox.confirm(
    '确认恢复该黑名单记录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 实现恢复逻辑
    ElMessage.success('恢复成功')
    handleSearch()
  })
}

// 详情
const handleDetail = (row) => {
  detail.value = row
  detailVisible.value = true
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
</script>

<style scoped>
.blacklist {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-card__body) {
  padding: 20px;
}
</style> 